import { Notification } from 'element-ui'

/**
 * 地图模块 - 预报预警
 */
export default {
    data() {
        return {
            legend: [
                // { name: '紫', icon: require('@/assets/map/village/0.png') },
                { name: '红', color: '#E22222', icon: require('@/assets/map/village/1.png') },
                { name: '橙', color: '#FF8D0C', icon: require('@/assets/map/village/2.png') },
                { name: '黄', color: '#F0C90B', icon: require('@/assets/map/village/3.png') },
                { name: '蓝', color: '#00B2FF', icon: require('@/assets/map/village/4.png') }
            ],
            rivlWidth: 2,
            _rivlImg: require('@/assets/map/poly-rivers.png'),
            paramsYY_YJ_LJ: null
        }
    },
    // 计算属性
    computed: {
        // 流域编码
        basin() {
            return this.$store.getters.basin
        }
    },
    // 生命周期-创建
    created() {
        this.$eventBus.$on('flyToVillage_YY_YJ_LJ', this.flyToVillShowPopYY_YJ_LJ)
        this.$eventBus.$on('EB_YY_YJ_LJ', params => {
            if (params) {
                this.creatYY_YJ_LJ(params)
            } else {
                this.clearYY_YJ_LJ()
            }
        })
        this.entitiesPonitYY_YJ_LJ = new Map()
        this._dataSourceRivlYY_YJ_LJ = []
        this._dataSourceLineYY_YJ_LJ = new Cesium.CustomDataSource('lineYY_YJ_LJ')
    },
    // 生命周期-销毁前
    beforeDestroy() {
    },
    // 生命周期-销毁
    destroyed() {
        // 清理
        this.clearYY_YJ_LJ()
    },
    // 生命周期-挂载
    mounted() {
    },
    methods: {
        creatYY_YJ_LJ(params) {
            this.clearYY_YJ_LJ()
            params = {
                dtBegin: '2022-07-01T12:00:0',
                dtEnd: '2022-07-31T12:00:00',
                wswscd: 'WDA8000126100000' }
            this.paramsYY_YJ_LJ = params
            var urlApi = '/api/EarlyWarning/GetForcastWarningForHis'
            this.$api.Get(urlApi, params).then(res => {
                console.log(res)
                res.data.forEach(item => {
                    var lv = Number(item.WLevel) - 1
                    var legend = this.legend[lv]
                    var prop = item
                    prop.dtBegin = params.dtBegin
                    prop.dtEnd = params.dtEnd
                    if (item.ewType === 1) {
                        // 村庄

                        var entity = {
                            eventName: 'YY_YJ_LJ',
                            prop: item,
                            position: Cesium.Cartesian3.fromDegrees(
                                Number(item.Lgtd),
                                Number(item.Lttd)
                            ),
                            billboard: {
                                image: legend.icon,
                                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                                verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, this.$config.mapConfig.pb_far),
                                width: this.$config.mapConfig.billboardSize,
                                height: this.$config.mapConfig.billboardSize
                            }
                        }
                        this.entitiesPonitYY_YJ_LJ.set(item.Ewcode, this.$viewer.entities.add(entity))
                    } else if (item.ewType === 3) {
                        var obj = {
                            ewcode: item.Ewcode,
                            color: legend.color,
                            item: item
                        }
                        // 河道
                        this.$eventBus.$emit('EB_YJ_LJ_CardSel_Res_RIVE', obj)
                        this._dataSourceRivlYY_YJ_LJ.push(obj)
                    }
                })
                this.initEvevtYY_YJ_LJ()
            })
        },

        /**
         * 模块事件初始化
         */
        initEvevtYY_YJ_LJ() {
            var eventFn = (pickedObject, position) => {
                if (
                    pickedObject &&
                    pickedObject.id &&
                    pickedObject.id.eventName
                ) {
                    if (pickedObject.id.eventName === 'YY_YJ_LJ') {
                        this.flyToVillShowPopYY_YJ_LJ(pickedObject.id.prop.Ewcode)
                    }
                    if (pickedObject.id.eventName === 'RIVL临近预警') {
                        // var cartesian = this.$viewer.camera.pickEllipsoid(event.position, this.$viewer.scene.globe.ellipsoid)
                        var cartesian = this.$viewer.camera.pickEllipsoid(position, this.$viewer.scene.globe.ellipsoid)
                        if (cartesian) {
                            // 将笛卡尔三维坐标转为地图坐标（弧度）
                            var cartographic = this.$viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian)
                            // 将地图坐标（弧度）转为十进制的度数
                            pickedObject.id.propByYbyj.lgtd = Number(Cesium.Math.toDegrees(cartographic.longitude).toFixed(5))
                            pickedObject.id.propByYbyj.lttd = Number(Cesium.Math.toDegrees(cartographic.latitude).toFixed(5))
                        }
                        const popStayParams = this.$store.getters.popStayParams
                        // if (!popParams.disable) { // 上一个对话框没有提交解禁
                        pickedObject.id.propByYbyj.eventName = 'RIVL临近预警'
                        popStayParams.name = '河道预警信息'
                        // popStayParams.posX = Math.round(event.position.x)
                        // popStayParams.posY = Math.round(event.position.y)
                        popStayParams.remarks = pickedObject.id.propByYbyj
                        this.$store.dispatch('popStayParams', popStayParams)
                        // this.$eventBus.$emit('flyToRIVL', pickedObject.id.prop.Ewcode)
                    }
                }
            }
            this.$store.getters.eventCesiumOption.leftPickFns.set('YY_YJ_LJ', eventFn)
        },
        flyToVillShowPopYY_YJ_LJ(adcd) {
            var entity = this.entitiesPonitYY_YJ_LJ.get(adcd)
            if (entity) {
                this.$viewer.flyTo(entity, {
                    duration: 0.5,
                    offset: {
                        heading: this.$viewer.camera.heading,
                        pitch: this.$viewer.camera.pitch,
                        range: 5000
                    }
                })
            }
            const popStayParams = this.$store.getters.popStayParams
            entity.prop.eventName = 'YY_YJ_LJ'
            entity.prop.rainStart = this.paramsYY_YJ_LJ.dtBegin
            entity.prop.rainEnd = this.paramsYY_YJ_LJ.dtEnd
            popStayParams.name = '村庄预警信息'
            popStayParams.remarks = entity.prop
            this.$store.dispatch('popStayParams', popStayParams)
        },

        /**
         * 模块清理
         */
        clearYY_YJ_LJ() {
            if (this.entitiesPonitYY_YJ_LJ && this.entitiesPonitYY_YJ_LJ.size > 0) {
                this.entitiesPonitYY_YJ_LJ.forEach((val, key, map) => {
                    this.$viewer.entities.remove(val)
                    map.delete(key)
                })
                // this.entitiesPonitYY_YJ_LJ = new Map()
            }
            this._dataSourceRivlYY_YJ_LJ = []
            this.$eventBus.$emit('resetRIVLColor')
            this.$store.dispatch('closePop')
            this.$store.dispatch('closePopStay')

            this.$store.getters.eventCesiumOption.leftPickFns.delete('YY_YJ_LJ')
        }
    }
}
